package nl.scalasim
package actors
package traits

import scala.actors.Actor

/**
 * The housing actor trait provides housing for a number of citizens.
 * It responds to AddResident message by sending an Available message.  
 *
 * @author Yigal Duppen
 * @since Oct 7, 2010
 */
trait HousingActor extends SimulationActor {

  val maxResidents: Int

  private[traits] override def residents = Amount(_residents.size, maxResidents)

  private var _residents = List[SimulationActor]()   

  def handleLiveAt: PartialFunction[Any, Unit] = {
    case LiveAt(resident, house) if _residents.length == maxResidents => {
      reply(Available(0))
    }

    case LiveAt(resident, house) => {
      _residents :+= resident
      addRelated(resident)
      reply(Available(maxResidents - _residents.length))
    }
  }

  callbacks :+= handleLiveAt
}